﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Masuit.Tools.DateTimeExt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class RiChangShiYongXinXiController : BaseController
    {
        private static int Km = 0;
        private static int Ci = 0;
        private readonly ILogger<RiChangShiYongXinXiController> _logger;
        public RiChangShiYongXinXiController(ILogger<RiChangShiYongXinXiController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long bzzbtid, [FromForm] long bzzbid, [FromForm] long jtid, [FromForm] string content, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            long total = 0;
            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
  'Logic' : 'Or',
  'Filters' :
  [
    {
      'Field' : '" + (bzzbtid == 1 ? "Clmc" : "Sbmc") + @"',
      'Operator' : 'Contains',
      'Value' : '" + content + @"',
    }
  ]
}
");
            
            #region 以前代码
            List<RiChangShiYongXinXi> richangshiyongxinxis = new();
            if (bzzbtid == 1)
            {// 车辆
                if (bzzbid == 2) {
                    JiaYouChe jiayouche = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == jtid).WhereDynamicFilter(dyfilter).Count(out total).Page(pageNumber, pageSize).ToOneAsync();
                    if (jiayouche != null)
                    {
                        List<DingQiKongZhiXiang> dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid && dqkzx.Bzzbid == bzzbid && dqkzx.Jtid == jtid && dqkzx.Gjid == 0).ToListAsync();

                        if (dingqikongzhixiangs != null)
                        {
                            foreach (var dingqikongzhixiang in dingqikongzhixiangs)
                            {
                                RiChangShiYongXinXi richangshiyongxinxi = new();
                                richangshiyongxinxi.Bzzbtid = 1;
                                richangshiyongxinxi.Bzzbid = 2;
                                richangshiyongxinxi.Jtid = jiayouche.Id;
                                richangshiyongxinxi.Zbwybm = jiayouche.Bzclwybm;
                                richangshiyongxinxi.Name = jiayouche.Clmc;
                                richangshiyongxinxi.Xh = jiayouche.Xh;
                                richangshiyongxinxi.Cph = jiayouche.Cph;
                                //JiaYouCheIOT jiayoucheiot = _fsql.Select<JiaYouCheIOT>().Where(jyciot => jyciot.Jtid == jiayouche.Id).OrderBy("id desc").Limit(1).ToOne();
                                BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == bzzbid && bzzbiot.Jtid == jtid && bzzbiot.Gjid == 0).OrderBy("id desc").ToOneAsync();
                                richangshiyongxinxi.Syrq = baozhangzhuangbeiiot.Created;
                                richangshiyongxinxi.Sydd = baozhangzhuangbeiiot.Zxwz;
                                richangshiyongxinxi.Sysj = jiayouche.Sysj;
                                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                                richangshiyongxinxi.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                                richangshiyongxinxi.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                                richangshiyongxinxi.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                                richangshiyongxinxi.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                                richangshiyongxinxi.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                                richangshiyongxinxi.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                                richangshiyongxinxi.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                                richangshiyongxinxi.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                                richangshiyongxinxis.Add(richangshiyongxinxi);
                            }
                        }
                    }
                }
                if (bzzbid == 1) {
                    DianYuanChe dianyuanche = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == jtid).WhereDynamicFilter(dyfilter).Count(out total).Page(pageNumber, pageSize).ToOneAsync();
                    if (dianyuanche != null)
                    {
                        List<DingQiKongZhiXiang> dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid && dqkzx.Bzzbid == bzzbid && dqkzx.Jtid == jtid && dqkzx.Gjid == 0).ToListAsync();
                        _logger.LogInformation("日常维护信息：" + _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid && dqkzx.Bzzbid == bzzbid && dqkzx.Jtid == jtid && dqkzx.Gjid == 0).ToSql());
                        if (dingqikongzhixiangs != null)
                        {
                            foreach (var dingqikongzhixiang in dingqikongzhixiangs)
                            {
                                RiChangShiYongXinXi richangshiyongxinxi = new();
                                richangshiyongxinxi.Bzzbtid = 1;
                                richangshiyongxinxi.Bzzbid = 2;
                                richangshiyongxinxi.Jtid = dianyuanche.Id;
                                richangshiyongxinxi.Zbwybm = dianyuanche.Bzclwybm;
                                richangshiyongxinxi.Name = dianyuanche.Clmc;
                                richangshiyongxinxi.Xh = dianyuanche.Xh;
                                richangshiyongxinxi.Cph = dianyuanche.Cph;
                                BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == bzzbid && bzzbiot.Jtid == jtid && bzzbiot.Gjid == 0).OrderBy("id desc").ToOneAsync();
                                richangshiyongxinxi.Syrq = baozhangzhuangbeiiot.Created;
                                richangshiyongxinxi.Sydd = baozhangzhuangbeiiot.Zxwz;
                                richangshiyongxinxi.Sysj = dianyuanche.Sysj;
                                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                                richangshiyongxinxi.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                                richangshiyongxinxi.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                                richangshiyongxinxi.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                                richangshiyongxinxi.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                                richangshiyongxinxi.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                                richangshiyongxinxi.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                                richangshiyongxinxi.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                                richangshiyongxinxi.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                                richangshiyongxinxis.Add(richangshiyongxinxi);
                            }
                        }
                    }
                }
            }
            else if (bzzbtid == 2)
            { // 设备
                BaoZhangSheBei baozhangshebei = await _fsql.Select<BaoZhangSheBei>().Where(bzsb => bzsb.Id == jtid).WhereDynamicFilter(dyfilter).Count(out total).Page(pageNumber, pageSize).ToOneAsync();
                if (baozhangshebei != null)
                {
                    List<DingQiKongZhiXiang> dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid && dqkzx.Bzzbid == bzzbid && dqkzx.Jtid == jtid && dqkzx.Gjid == 0).ToListAsync();
                    if (dingqikongzhixiangs != null)
                    {
                        foreach (var dingqikongzhixiang in dingqikongzhixiangs)
                        {
                            RiChangShiYongXinXi richangshiyongxinxi = new();
                            richangshiyongxinxi.Bzzbtid = 1;
                            richangshiyongxinxi.Bzzbid = 2;
                            richangshiyongxinxi.Jtid = baozhangshebei.Id;
                            richangshiyongxinxi.Zbwybm = baozhangshebei.Bzsbwybm;
                            richangshiyongxinxi.Name = baozhangshebei.Sbmc;
                            richangshiyongxinxi.Xh = baozhangshebei.Xh;
                            richangshiyongxinxi.Cph = "";
                            //BaoZhangSheBeiIOT baozhangshebeiiot = _fsql.Select<BaoZhangSheBeiIOT>().Where(bzsbiot => bzsbiot.Jtid == baozhangshebei.Id).OrderBy("id desc").Limit(1).ToOne();
                            BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == bzzbid && bzzbiot.Jtid == jtid && bzzbiot.Gjid == 0).OrderBy("id desc").ToOneAsync();
                            richangshiyongxinxi.Syrq = baozhangzhuangbeiiot.Created;
                            richangshiyongxinxi.Sydd = baozhangzhuangbeiiot.Zxwz;
                            richangshiyongxinxi.Sysj = baozhangshebei.Sysj;
                            JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                            richangshiyongxinxi.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                            richangshiyongxinxi.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                            richangshiyongxinxi.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                            richangshiyongxinxi.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                            richangshiyongxinxi.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                            richangshiyongxinxi.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                            richangshiyongxinxi.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                            richangshiyongxinxi.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                            richangshiyongxinxis.Add(richangshiyongxinxi);
                        }
                    }
                }
            }
            #endregion

            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(richangshiyongxinxis));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] RiChangShiYongXinXi richangshiyongxinxi)
        {
            long id = await _fsql.Insert(richangshiyongxinxi).ExecuteIdentityAsync();
            richangshiyongxinxi.Id = id;

            return RenderJson(data: richangshiyongxinxi);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id)
        {
            int affrows = await _fsql.Delete<RiChangShiYongXinXi>().Where(rcsyxx => rcsyxx.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] RiChangShiYongXinXi richangshiyongxinxi)
        {
            int affrows = await _fsql.Update<RiChangShiYongXinXi>().SetSourceIgnore(richangshiyongxinxi, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id)
        {
            RiChangShiYongXinXi richangshiyongxinxi = await _fsql.Select<RiChangShiYongXinXi>().Where(rcsyxx => rcsyxx.Id == id).ToOneAsync();
            return RenderJson(data: richangshiyongxinxi);
        }
    }
}
